package net.easyjava.third.db.mysql;

import net.easyjava.third.db.base.DBClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;

/**
 * Created by wxnacy on 16/6/24.
 */
public class MysqlClient extends DBClient {
    private static Logger logger = LoggerFactory.getLogger(MysqlClient.class);
    /**
     * 构造单例
     */
    private volatile static MysqlClient singleton;
    private MysqlClient(final String jdbcUrl,final String jdbcName,final String jdbcPassword) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        this.jdbcUrl = jdbcUrl;
        this.jdbcName = jdbcName;
        this.jdbcPassword = jdbcPassword;
        createConn();

    }
    public static MysqlClient getInstance(final String jdbcUrl,final String jdbcName,final String jdbcPassword) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        if (singleton == null) {
            synchronized (MysqlClient.class) {
                if (singleton == null) {
                    System.out.println("MysqlClient.getInstance");
                    singleton = new MysqlClient(jdbcUrl,jdbcName, jdbcPassword);
                }
            }
        }
        if(cvConn == null){
            createConn();
        }
        return singleton;
    }
    private static synchronized void createConn() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        if(cvConn == null){
            System.out.println("MysqlClient.createConn");
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                init();
        }

    }


    public static void main(String[] args) {
        String jdbcUrl = "jdbc:mysql://mingjuer.mysql.rds.aliyuncs.com:3306/mediacenter";
        String jdbcName = "juer";
        String jdbcPassword = "Juer2016";
        MysqlClient client = null;
        try {
            client = MysqlClient.getInstance(jdbcUrl, "juer", "Juer2016");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
//        List<JSONObject> list = client.query("SELECT * FROM VIDEO where id=106");
        long begin = System.currentTimeMillis();
//        String sql = "INSERT INTO CATEGORY (cid, name, status, display_order) VALUES (?,?,?,?);";
        String sql = "UPDATE CATEGORY SET name = '河北梆子啊' WHERE id = 50;";
        boolean flag = false;
        try {
            flag = client.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("(System.currentTimeMillis()-begin) = " + (System.currentTimeMillis() - begin));
        System.out.println("flag = " + flag);
        client.destroy();
        try {
            client = MysqlClient.getInstance(jdbcUrl, "juer", "Juer2016");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
//        List<JSONObject> list = client.query("SELECT * FROM VIDEO where id=106");
        begin = System.currentTimeMillis();
//        String sql = "INSERT INTO CATEGORY (cid, name, status, display_order) VALUES (?,?,?,?);";
        sql = "UPDATE CATEGORY SET name = '河北梆子' WHERE id = 50;";
        try {
            flag = client.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("(System.currentTimeMillis()-begin) = " + (System.currentTimeMillis() - begin));
        System.out.println("flag = " + flag);
        client.destroy();
    }





}
